<?php
	class PlansController extends PaidListingsAppController
	{
		var $name = "Plans";
		
		function admin_add()
		{
			if($this->data)
			{
				$this->Plan->create();
				if($this->Plan->save($this->data))
				{
					$this->successMsg(__("Plan has been added successfully"));
					$this->redirect(array("action"=>"index"));
				}else
				{
					$this->errorMsg(__("Please fix the error below."));
				}
			}	 
		}
		
		function admin_edit($planId)
		{
			$plan = $this->Plan->findById($planId);
			if(!$plan)
			{
				$this->redirect(array("action"=>"index"));
			}
			if($this->data)
			{
				$this->Plan->create();
				$this->data['Plan']['id'] = $planId;
				if($this->Plan->save($this->data))
				{
					$this->successMsg(__("Plan has been saved"));
					$this->redirect(array("action"=>"index"));
				}else
				{
					$this->errorMsg(__("Please fix the errors."));
				}
			}else
			{
				$this->data = $plan;
				$this->data['Plan']['categories'] = implode(",",$this->data['Plan']['categories']);
				$this->data['Plan']['custom_fields']['fieldsets'] = implode(",",$this->data['Plan']['custom_fields']['fieldsets']);
				$this->data['Plan']['custom_fields']['fields'] = implode(",",$this->data['Plan']['custom_fields']['fields']);
			}
			$this->render("admin_add");
		}
		function admin_index()
		{
			$this->paginate['limit'] = 20;
			$plans = $this->paginate();
			$this->set("plans",$plans);
			$this->helpers[] = "Index";
		}
		
		function admin_delete($id)
		{
			if(!$id)
			{
				$this->redirect(array("action"=>"index"));
			}
			if($this->Plan->del($id))
			{
				$this->successMsg(__("Plan has been deleted"));
			}
			$this->redirect(array("action"=>"index"));
		}
		function admin_getFields()
		{
			$categories = $_POST['categories'];
			$categories = explode(",",$categories);
			$allCategories = array();
			$ListCategory = cri("ListCategory");
			array_filter($categories);
			foreach($categories as $categoryId)
			{
				if(!$categoryId)
				{
					continue;
				}
				$childrenCategories = $ListCategory->getChildrenOptions($categoryId);
				$childrenCategories = array_keys($childrenCategories);
				$allCategories[] = $categoryId;
				$allCategories = am($allCategories,$childrenCategories);
			}
			$allCategories = array_unique($allCategories);
			$listingTypeIds = $ListCategory->find('list',array("fields"=>array('id',"listing_type_id"),'conditions'=>array($ListCategory->alias.'.id'=>$allCategories)));
			$listingTypeIds = array_unique($listingTypeIds);
			$fieldsetIds = cri("ListingTypeFieldset")->find('list',array("conditions"=>array('listing_type_id'=>$listingTypeIds),'fields'=>array('fieldset_id')));
			$fieldsets = cri("Fieldset")->find("all",array("recursive"=>-1,'conditions'=>array("id"=>$fieldsetIds,'entity'=>Fieldset::TYPE_LISTING)));
			$fieldsetIds = Set::extract($fieldsetIds,'{n}.Fieldset.id');
			$customFields = cri("CustomField")->find("all",array("recursive"=>-1,'conditions'=>array("fieldset_id"=>$fieldsetIds)));
			$this->set(compact('fieldsets','customFields'));
			$this->layout = false;		
		}
	}
?>